<?php
namespace app\admin\logic;

use app\common\logic\Base;
use think\Db;
use think\facade\App;

class Url extends Base {

    /**
     * @desc 我的链接
     * @param array $params
     * @param int $userId
     * @return array
     * @throws \Exception
     */
    public function index($params) {
        $where = [];
        if($params['keyword']) {
            $where[] = ['keyword', 'like', '%'.$params['keyword'].'%'];
        }

        if($params['link']) {
            $where[] = ['link', 'like', '%'.$params['link'].'%'];
        }

        if($params['userName']) {
            $userId = App::model('User')
                ->where('user_phone', 'like', '%'.$params['userName'].'%')
                ->whereOr('user_email', 'like', '%'.$params['userName'].'%')
                ->value('user_id');
            if($userId) {
                $where[] = ['user_id', '=', $userId];
            }
        }

        if ($params['startDate'] && $params['endDate']) {
            $startTime = date('Y-m-d 00:00:00', strtotime($params['startDate']));
            $endTime = date('Y-m-d 23:59:59', strtotime($params['endDate']));
            $where[] = ['create_time', 'between', [$startTime, $endTime]];
        }else{
            if($params['startDate']) {
                $where[] = ['create_time', 'gt', date('Y-m-d 00:00:00', strtotime($params['startDate']))];
            }
            if($params['endDate']) {
                $where[] = ['create_time', 'lt', date('Y-m-d 23:59:59', strtotime($params['endDate']))];
            }
        }

        $list = App::model('Url')
            ->where($where)
            ->order('url_id desc')
            ->paginate($params['pageSize'], false, ['query' => request()->param()]);

        $pagination = $list->render();
        if($list) {
            $list = $list->toArray();
            $list = output_format($list['data']);

            $userIds = array_unique(array_column($list, 'userId'));
            $userInfo = App::model('User')
                ->where('user_id', 'in', $userIds)
                ->column('user_phone,user_email', 'user_id');
            foreach ($list as $k => $v) {
                $list[$k]['userName'] = $userInfo[$v['userId']]['user_phone'] ?: $userInfo[$v['userId']]['user_email'];
            }
        }

        return [
            'list'         => $list,
            'params'       => $params,
            'pagination'   => $pagination
        ];
    }

    /**
     * @desc 我的链接
     * @param array $params
     * @param int $userId
     * @return array
     * @throws \Exception
     */
    public function log($params) {
        $where = [];
        if($params['keyword']) {
            $where[] = ['short_url', 'like', '%'.$params['keyword'].'%'];
        }

        if($params['shortUrl']) {
            $where[] = ['short_url', 'like', '%'.$params['shortUrl'].'%'];
        }

        if ($params['startDate'] && $params['endDate']) {
            $startTime = date('Y-m-d 00:00:00', strtotime($params['startDate']));
            $endTime = date('Y-m-d 23:59:59', strtotime($params['endDate']));
            $where[] = ['create_time', 'between', [$startTime, $endTime]];
        }else{
            if($params['startDate']) {
                $where[] = ['create_time', 'gt', date('Y-m-d 00:00:00', strtotime($params['startDate']))];
            }
            if($params['endDate']) {
                $where[] = ['create_time', 'lt', date('Y-m-d 23:59:59', strtotime($params['endDate']))];
            }
        }

        $list = model('UrlLog')
            ->where($where)
            ->order('id desc')
            ->paginate($params['pageSize'], false, ['query' => request()->param()]);

        $pagination = $list->render();
        if($list) {
            $list = $list->toArray();
            $list = output_format($list['data']);
        }

        return [
            'list'         => $list,
            'params'       => $params,
            'pagination'   => $pagination
        ];
    }
}